package DCSource.ConfigManager;

import DCSource.ConfigManager.Item;
import java.util.ArrayList;

public class Class{
	private String decompile(String Text){
		Text = Text.replace("{&008;}","{&001;}{&007;}");
		Text = Text.replace("{&001;}",String.valueOf((char) 13));
		Text = Text.replace("{&002;}","=");
		Text = Text.replace("{&003;}",";;");
		Text = Text.replace("{&004;}",";-;");
		Text = Text.replace("{&005;}",";/;");
		Text = Text.replace("{&006;}","main");
		Text = Text.replace("{&007;}",String.valueOf((char) 10));
		Text = Text.replace("{&009;}",";:;");
		Text = Text.replace("{&010;}","name");
		Text = Text.replace("{&011;}",";.;");
		Text = Text.replace("{&012;}","version");
		Text = Text.replace("{&013;}", ";MG;");
		Text = Text.replace("{&014;}","[");
		Text = Text.replace("{&015;}", "]");
		return Text;
	}
	private int rand(int low, int high) {  
        high++;  
        return (int) (Math.random() * (high - low) + low);  
    }
	private boolean isWindowsSystem()
	  { String osName = System.getProperty("os.name").toLowerCase();
	    return osName.indexOf("windows") >= 0;
	  }
	  
	private boolean isLinuxSystem()
	  { String osName = System.getProperty("os.name").toLowerCase();
	    return osName.indexOf("linux") >= 0;
	  }
	private String umsch(){
		if (isWindowsSystem()==true){
			return String.valueOf((char)13)+String.valueOf((char)10);
		}else{return String.valueOf((char)10);}
	}
	private ArrayList<Item> list=new ArrayList();
	private String name = rand(0,999999999)+""+rand(0,999999999);
	public Class() {}
	public Class(String title) {
		name = (title);
	}
	public Class(String title,Item item) {
		name = (title);
		list.add(item);
	}
	public Class(Item[] mgs){
		for (int I=0;I<mgs.length;I++){
			list.add(mgs[I]);
		}
	}
	public Class(String title, Item[] mgs){
		name = (title);
		for (int I=0;I<mgs.length;I++){
			list.add(mgs[I]);
		}
	}
	public boolean addItem(Item item,boolean overwrite){
		boolean found=false;
		boolean doing=false;
		for (int I=0;I<list.size();I++){
			if (list.get(I).getTitle().equals(item.getTitle())){
				if (overwrite==true){
					list.set(I,item);
					doing=true;
					found=true;
				}else{found=true;}
			}
		}
		if (found==false){
			list.add(item);
			doing=true;
		}
		return doing;
	}
	public boolean addItem(String title,String value,boolean overwrite){
		boolean found=false;
		boolean doing=false;
		for (int I=0;I<list.size();I++){
			if (list.get(I).getTitle().equals(title)){
				if (overwrite==true){
					list.set(I,new Item(title,value));
					doing=true;
					found=true;
				}else{found=true;}
			}
		}
		if (found==false){
			list.add(new Item(title,value));
			doing=true;
		}
		return doing;
	}
	public void compare(Item[] toAdd, boolean overwrite){
		for (Item it : toAdd){
			this.addItem(it, overwrite);
		}
	}
	public void compare(Class c, boolean overwrite){
		for (Item it : c.getall()){
			this.addItem(it, overwrite);
		}
	}
	public Item getItemByDoingstr(String str){
		Item b = new Item();
		String[] dec = str.split("=");
		if (dec.length>1){
			String zw = "";
			for (int I=1;I<dec.length;I++){
				if (I==1){
					zw+=dec[I];
				}else{
					zw+="="+dec[I];
				}
			}
			b=(new Item(decompile(dec[0]),zw));
		}else{
			b=(new Item(decompile(str)));
		}
		return b;
	}
	public Item[] getItemsByDoingstr(String[] strs){
		Class decoder = new Class();
		for (String str : strs){
			Item b = new Item();
			String[] dec = str.split("=");
			if (dec.length>1){
				String zw = "";
				for (int I=1;I<dec.length;I++){
					if (I==1){
						zw+=dec[I];
					}else{
						zw+="="+dec[I];
					}
				}
				b=(new Item(decompile(dec[0]),zw));
			}else{
				b=(new Item(decompile(str)));
			}
			decoder.addItem(b,false);
		}
		return decoder.getall();
	}
	public Item[] getItemsByDoingstr(String[] strs, boolean overwrite){
		Class decoder = new Class();
		for (String str : strs){
			Item b = new Item();
			String[] dec = str.split("=");
			if (dec.length>1){
				String zw = "";
				for (int I=1;I<dec.length;I++){
					if (I==1){
						zw+=dec[I];
					}else{
						zw+="="+dec[I];
					}
				}
				b=(new Item(decompile(dec[0]),zw));
			}else{
				b=(new Item(decompile(str)));
			}
			decoder.addItem(b,overwrite);
		}
		return decoder.getall();
	}
	public void revItem(Item item){
		for (int I=0;I<list.size();I++){
			if (list.get(I).getTitle().equals(item.getTitle())){
				list.remove(I);
				I--;
			}
		}
	}
	public void revItem(String title){
		for (int I=0;I<list.size();I++){
			if (list.get(I).getTitle().equals(title)){
				list.remove(I);
				I--;
			}
		}
	}
	public void revItem(int dex){
		if (dex<list.size() & dex>-1){
			list.remove(dex);
		}
	}
	public void revAllItems(){
		list.clear();
	}
	public Item getItem(String title){
		Item returning=new Item("");
		for (int I=0;I<list.size();I++){
			if (list.get(I).getTitle().equals(title)){
				returning = list.get(I);
			}
		}
		return returning;
	}
	public Item getItem(int dex){
		Item returning=new Item("");
		if (dex<list.size() & dex>-1){
			returning=list.get(dex);
		}
		return returning;
	}
	public Item[] getall(){
		return list.toArray(new Item[list.size()]);
	}
	public int getCount(){
		return list.size();
	}
	public void setTitle(String title){
		name=(title);
	}
	public String getTitle(){
		if (name!=null){
		return (name);}else{
		return ("");
		}
	}
	public void Clear(){
		list.clear();
	}
	public boolean existItem(String title){
		boolean returning=false;
		for (int I=0;I<list.size();I++){
			if (list.get(I).getTitle().equals(title)){
				returning=true;
			}
		}
		return returning;
	}
	public boolean existItem(Item it){
		boolean returning=false;
		for (int I=0;I<list.size();I++){
			if (list.get(I).getTitle().equals(it.getTitle())){
				returning=true;
			}
		}
		return returning;
	}
	public String toString(){
		String text = name+">>";
		for (int I=0;I<list.size();I++){
			text += list.get(I).toString()+";";
		}
		return "["+text+"]";
	}
}